package cn.youwan.market;


import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
import cn.youwan.market.content.ContentData;
import cn.youwan.market.utils.AnimCommon;

@SuppressLint("NewApi")
public class DrawerActivity extends ActivityGroup implements OnClickListener{

	private DrawerLayout mDrawerLayout;
	//	private ListView mDrawerList;
	private LinearLayout mDrawerLeft;
	private ListView mDrawerListRight;
	// private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] mPlanetTitles;
	private FrameLayout container;
	public boolean isFirst = true;
	private TextView tv_collection;
	private TextView tv_history;
	private TextView tv_message_remind;
	private TextView tv_firends_news;
	private TextView tv_team_meetings;
    public static List<String> lists=new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drawer);

		isFirst = ContentData.isFirstOpen(DrawerActivity.this);
		if (isFirst) {
			Intent intent = new Intent(this, WelcomeActivity.class);
			startActivity(intent);
			ContentData.saveFirstOpen(this);
			this.finish();
		}

		if(!isFirst){
			Intent intent=new Intent(this,RecommendActivity.class);
			startActivity(intent);
		}


		tv_collection=(TextView)findViewById(R.id.tv_collection);
		tv_history=(TextView)findViewById(R.id.tv_history);
		tv_message_remind=(TextView)findViewById(R.id.tv_message_remind);
		tv_firends_news=(TextView)findViewById(R.id.tv_firends_news);
		tv_team_meetings=(TextView)findViewById(R.id.tv_team_meetings);
		
        tv_collection.setOnClickListener(this);
        tv_history.setOnClickListener(this);
        tv_message_remind.setOnClickListener(this);
        tv_firends_news.setOnClickListener(this);
        tv_team_meetings.setOnClickListener(this);



		mTitle = mDrawerTitle = getTitle();
		mPlanetTitles = getResources().getStringArray(R.array.planets_array);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLeft = (LinearLayout) findViewById(R.id.left_drawer);
		mDrawerListRight = (ListView) findViewById(R.id.right_drawer);


		// set a custom shadow that overlays the main content when the drawer
		// opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		// set up the drawer's list view with items and click listener
		//		mDrawerList.setAdapter(new ArrayAdapter<String>(this,
		//				R.layout.drawer_list_item, mPlanetTitles));
		//		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

		mDrawerListRight.setAdapter(new ArrayAdapter<String>(this,
				R.layout.drawer_list_item, mPlanetTitles));
		mDrawerListRight.setOnItemClickListener(new DrawerItemClickListener());

		// enable ActionBar app icon to behave as action to toggle nav drawer
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setTitle("");

		getActionBar().setDisplayHomeAsUpEnabled(false);
		getActionBar().setIcon(R.drawable.biz_ad_show_new_version_btn_pressed);
		Drawable barBg = getResources().getDrawable(R.drawable.barbg);
		getActionBar().setBackgroundDrawable(barBg);
		getActionBar().setCustomView(R.layout.test_search);
		getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM 
				| ActionBar.DISPLAY_SHOW_HOME);  

		if (savedInstanceState == null) {
			selectItem(0);
		}

		container = (FrameLayout) findViewById(R.id.content_frame);
		container.removeAllViews();
		container.addView(
				getLocalActivityManager().startActivity("MainActivity",
						new Intent(this, MainActivity.class)).getDecorView(),
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/* Called whenever we call invalidateOptionsMenu() */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// If the nav drawer is open, hide action items related to the content
		// view
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLeft);
		menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.
		// if (mDrawerToggle.onOptionsItemSelected(item)) {
		// return true;
		// }
		// Handle action buttons
		switch (item.getItemId()) {
		case R.id.action_websearch:
			if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
				mDrawerLayout.closeDrawer(mDrawerLeft);
			}
			if (mDrawerLayout.isDrawerOpen(mDrawerListRight)) {
				mDrawerLayout.closeDrawer(mDrawerListRight);
			} else {
				mDrawerLayout.openDrawer(mDrawerListRight);
			}
			return true;
		case android.R.id.home:
			if (mDrawerLayout.isDrawerOpen(mDrawerListRight)) {
				mDrawerLayout.closeDrawer(mDrawerListRight);
			}
			if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
				mDrawerLayout.closeDrawer(mDrawerLeft);
			} else {
				mDrawerLayout.openDrawer(mDrawerLeft);
			}

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements
	ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
		}
	}

	private void selectItem(int position) {
		mDrawerLayout.closeDrawer(mDrawerLeft);
		mDrawerLayout.closeDrawer(mDrawerListRight);

	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setIcon(R.drawable.biz_ad_show_new_version_btn_pressed);
	}

	/**
	 * When using the ActionBarDrawerToggle, you must call it during
	 * onPostCreate() and onConfigurationChanged()...
	 */

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onClick(View v) {
		container.removeAllViews();
		lists=new ArrayList<String>();
		Intent intent=new Intent(this, PersonalActivity.class);
		container.addView(
				getLocalActivityManager().startActivity("PersonalActivity",intent)
				.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		lists.add("PersonalActivity");
		switch(v.getId()){
		case R.id.tv_collection:
			PersonalActivity.instance.setCurrentPage(2);
			break;
		case R.id.tv_history:
			PersonalActivity.instance.setCurrentPage(3);
			break;
		case R.id.tv_message_remind:
			PersonalActivity.instance.setCurrentPage(1);
			break;
		case R.id.tv_firends_news:
			PersonalActivity.instance.setCurrentPage(0);
			break;
		case R.id.tv_team_meetings:
			PersonalActivity.instance.setCurrentPage(4);
			break;
		}
		mDrawerLayout.closeDrawer(mDrawerLeft);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
		if(lists.size()>0){
			if(lists.get(0).equals("PersonalActivity")){
				Intent intent=new Intent(this, MainActivity.class);
				container.removeAllViews();
				container.addView(
						getLocalActivityManager().startActivity("MainActivity",intent)
						.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				lists=new ArrayList<String>();
				return true;
			}
		}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	protected void onPause() {
		System.out.println("pause");
		if(AnimCommon.in!=0 && AnimCommon.out!=0){
			super.overridePendingTransition(AnimCommon.in, AnimCommon.out);
			AnimCommon.clear();
		}
		super.onPause();
	}
	
	
}
